package no.rmz.chez.tournament;

import java.util.Collection;
import java.util.LinkedList;
import java.util.Map;
import java.util.TreeMap;
import java.util.Map.Entry;

import no.rmz.chez.chess.board.ChessBoard;
import no.rmz.chez.game.Game;
import no.rmz.chez.players.ChessPlayer;


/**
 * A class encapsulating the results produced by a 
 * tournament.
 *
 */
public class TournamentResult {
    
    @Override
    /**
     * Override the standard toString method. Mostly for
     * debugging purposes.
     */
    public String toString() {
        final StringBuilder sb = new StringBuilder();
        for (Entry<PlayerPair,  Collection<Game>> entry : tournamentResults.entrySet()) {
            PlayerPair pp = entry.getKey();
            sb.append("  ").append(pp.getP1()).append(" v.s. ").append(pp.getP2()).append(":").append(ChessBoard.NEWLINE);
            for (Game g : entry.getValue()) {
                sb.append("   ").append(g.toString());
            }
        }
        return sb.toString();
    }
    
    
    /**
     * A map for collecting tournament results.
     */
    private final Map<PlayerPair, Collection<Game>>  tournamentResults =
        new TreeMap<>();

    /**
     * Add a game result to the tournament results.
     *   XXXX This is bogus, should add whole game, annotated with game result!
     * @param w the white player.
     * @param b the black player.
     * @param g the game.
     */
    public final void addGameResult(
            final ChessPlayer    w,
            final ChessPlayer    b,
            final Game   g) {
        PlayerPair key = new PlayerPair(w, b);
        final Collection<Game> c;

        if (tournamentResults.containsKey(key)) {
            c = tournamentResults.get(key);
        } else {
            c = new LinkedList<>();
            tournamentResults.put(key, c);
        }
        c.add(g);
    }
}
